// part1:C12:ex12.14.cpp
// shared_ptr and connection.
#include <iostream>
#include <memory>
using namespace std;

struct destination {};
struct connection {};

connection connect(destination *pd)
{
  cout << "open connection" << endl;
  return connection();
}

void disconnection(connection c)
{
  cout << "close connection" << endl;
}

// unused shared_ptr method
void f(destination &d)
{
  cout << "directed manage connect" << endl;
  connection c = connect(&d);
  cout << endl;
}

void end_connction(connection *p)
{
  disconnection(*p);
}

// use shared_ptr
void f1(destination &d)
{
  cout << "shared_ptr manage connect" << endl;
  connection c = connect(&d);

  shared_ptr<connection> p(&c, end_connction);
  cout << endl;
}

int main(int argc, char *argv[])
{
  destination d;
  f(d);
  f1(d);

  return 0;
}
